/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.TelephonyManager;
import android.telephony.TelephonyProtoEnums;

import com.ecarx.systemui.plugin.controller.base.BaseController;
import com.ecarx.systemui.plugin.model.listener.MobileSignalCallBack;
import com.ecarx.systemui.plugin.utils.AppUtils;
import com.ecarx.systemui.plugin.utils.LogUtils;

public class MobileSignalController extends BaseController {
    private static final String TAG = "MobileSignalController";
    public static final String ACTION_SIGNAL_CHANGED = "android.telephony.action.SIGNAL_CHANGED";
    public static final String EXTRA_SIGNAL_STRENGTH_VALUE = "android.telephony.extra.SIGNAL_STRENGTH_VALUE";

    public static final String ACTION_NETWORK_TYPE_OR_STATE_CHANGED = "android.telephony.action.NETWORK_TYPE_OR_STATE_CHANGED";
    public static final String EXTRA_NETWORK_TYPE_VALUE = "android.telephony.extra.NETWORK_TYPE_VALUE";
    public static final String EXTRA_NETWORK_STATE_VALUE = "android.telephony.extra.NETWORK_STATE_VALUE";

    public MobileSignalController(Context context) {
        super(context);
    }

    @Override
    protected void initController(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_SIGNAL_CHANGED);
        filter.addAction(ACTION_NETWORK_TYPE_OR_STATE_CHANGED);
        AppUtils.registerReceiver(context, this, filter);
    }

    private MobileSignalCallBack mobileSignalCallBack;

    public void setMobileSignalCallBack(MobileSignalCallBack mobileSignalCallBack) {
        this.mobileSignalCallBack = mobileSignalCallBack;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (intent == null) {
            return;
        }
        String action = intent.getAction();
        LogUtils.d(TAG, "onReceive action:" + action);
        if (ACTION_SIGNAL_CHANGED.equals(action)) {
            int level = intent.getIntExtra(EXTRA_SIGNAL_STRENGTH_VALUE, -1);
            if (mobileSignalCallBack != null) {
                mobileSignalCallBack.onSignalChanged(level);
            }
        } else if (ACTION_NETWORK_TYPE_OR_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(EXTRA_NETWORK_STATE_VALUE, TelephonyManager.DATA_UNKNOWN);
            int type = intent.getIntExtra(EXTRA_NETWORK_TYPE_VALUE, TelephonyProtoEnums.NETWORK_TYPE_UNKNOWN);
            if (mobileSignalCallBack != null) {
                mobileSignalCallBack.onNetworkStateChanged(state, type);
            }
        }

    }

    @Override
    public void destroyController(Context context) {
        context.unregisterReceiver(this);
    }
}
